package com.ukao.pad.presenter;

import android.content.Context;

import com.ukao.pad.base.BasePresenter;
import com.ukao.pad.bean.StringBean;
import com.ukao.pad.bean.VipGroupBean;
import com.ukao.pad.bean.VipLeverBean;
import com.ukao.pad.consts.Constant;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.retrofit.ApiCallback;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.Utils;
import com.ukao.pad.view.CreateVipView;

import java.util.HashMap;

/**
 * Created by kefu001 on 2018/5/21.
 */

public class CreateVipPresenter  extends BasePresenter<CreateVipView>{
    public CreateVipPresenter(CreateVipView view) {
        super.attachView(view);
    }
    /**
     * 请求会员卡等级数据
     */
    public void requestVipCardLever(Context mContext  ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        addSubscription(apiStores.queryVipLeverList(Constant.createParameter(mHashMap)),
                new ApiCallback<VipLeverBean>() {
                    @Override
                    public void onSuccess(VipLeverBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadVipCardLeverSucceed(model.getData().getList());
                        }else{
                            mvpView.loadFailure(model.getMsg());
                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }
                    @Override
                    public void onFinish() {
                        requestAllGroup();
                    }

                });
    }
    /**
     *获取全部分组
     */
    public void requestAllGroup() {
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.queryAllDefaultGroup(Constant.createParameter(mHashMap)),
                new ApiCallback<VipGroupBean>() {
                    public void onSuccess(VipGroupBean model) {
                        if (model.getHttpCode()==200){
                            if (!CheckUtils.isEmpty(model.getData()))
                                mvpView.loadAllGroupSucceed(model.getData());
                        }else{
                            mvpView.loadFailure(model.getMsg());
                        }
                 vipUserGroupData();
                    }

                    @Override
                    public void onNoNewWork() {

                    }
                    @Override
                    public void onFinish() {

                    }

                });
    }


    /**
     *用戶分組
     */
    public void vipUserGroupData(   ) {
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.vipUserGroup(Constant.createParameter(mHashMap)),
                new ApiCallback<VipGroupBean>() {
                    @Override
                    public void onSuccess(VipGroupBean model) {

                        if (model.getHttpCode()==200){
                            if (!CheckUtils.isEmpty(model.getData()))
                                mvpView.loadvipGroupSucceed(model.getData());
                        }else{
                            mvpView.loadFailure(model.getMsg());
                        }

                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     * 保存会员信息
     * @param mHashMap
     */
    public void saveVipInfo(HashMap mHashMap) {
        mvpView.showLoading();
        addSubscription(apiStores.buildMember(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        mvpView.hideLoading();
                        if (model.getHttpCode() == 200) {
                            mvpView.buildUserSucceed();
                        } else {
                            mvpView.loadFailure(model.getMsg());
                        }

                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();

                    }
                });
    }
}
